<?php
if (!isset($acl) || !isset($curruser))
{
    exit();
}
$acltitle = isset($aclname) ? $aclname[$acl] : "";
if (SubAccountRight($curruser, 2) == 2)
{
    $fg = new Flag();
    $psen_edit_mod_Arr = array();
    $lotto_id_Arr = array();
    $open = date("Y-m-d");
    $amount_Arr = array("6h" => 0, "3d" => 0, "3s" => 0);
    $snow_mod_Arr = array();
    $protmod_Arr = array();
    $rate0 = 0;
    $rate0mod = 0;
    $rate8mod = 0;
    $mpercent_Arr = array();
    $percent_sw_Arr = array();
    $percent0_Arr = array();
    $percent1_Arr = array();
    $percent2_Arr = array();
    $amount_used_Arr = array();
    foreach ($g_gametype_3d as $gameid => $val)
    {
        $snow_mod_Arr[$gameid] = 0;
        $protmod_Arr[$gameid] = 0;
        $mpercent_Arr[$gameid] = 0;
        $percent0_Arr[$gameid] = 0;
        $percent1_Arr[$gameid] = 0;
        $percent2_Arr[$gameid] = 0;
        $percent_sw_Arr[$gameid] = 0;
        $amount_used_Arr[$gameid] = 0;
        $ary = $fg->swth_control($gameid);
        $psen_edit_mod_Arr[$gameid] = $ary[0];
        $lotto_id_Arr[$gameid] = $ary[1];
        if ($psen_edit_mod_Arr[$gameid])
        {
            $open = min($open, $ary[2]);
        }
        unset($ary);
    }
    unset($fg);
    $account = getvar("account");
    $pass = getvar("pass");
    $username = getslashes(getvar("name"));
    if ($username == "")
    {
        $username = $account;
    }

    /*if ($GLOBALS['g_conf']['language'] == "zh-cn")
    {
        include_once( "class/ccharset.php" );
        $cc = new CCharset();
        $username = $cc->Gb_big5($username);
    }*/
    if ($g_init_mode_6h && !$g_init_mode_3d)
    {
        $gameinit_mode = 2;
    }
    else if (!$g_init_mode_6h && $g_init_mode_3d)
    {
        $gameinit_mode = 3;
    }
    else
    {
        $gameinit_mode = getvar("gamemod");
        if ($gameinit_mode == "")
        {
            $gameinit_mode = 1;
        }
    }
    $enabled = getvar("enabled");
    if ($enabled == "")
    {
        $enabled = 1;
    }
    $mbr_disabled_chk = getvar("mbr_disabled") ? 1 : 0;
    //$super_account = getvar("sup_account");
	$amount_Arr['6h'] = intval(getvar("amount"));
    if (0 < $id)
    {
        $chk3DFlagArr = array("enabled" => 0, "totalAmounts" => 0);
        $chk3SFlagArr = array("enabled" => 0, "totalAmounts" => 0);
        foreach ($g_gametype_3d as $gameid => $val)
        {
            $g_gametype_3d_Enabled = 1;
            if ($g_init_mode_6h && $gameid == 0)
            {
                $rate0 = max(0, floatval(getvar("rate0")));
                $rate0mod = getvar("rate0mod") ? 1 : 0;
                $rate8mod = getvar("rate8mod") ? 1 : 0;
                
                $snow_mod_Arr[$gameid] = getvar("snow_mod") ? 1 : 0;
                $protmod_Arr[$gameid] = getvar("protmod") ? 1 : 0;
                $mpercent_Arr[$gameid] = intval(getvar("mpercent"));
                $percent0_Arr[$gameid] = intval(getvar("percent0"));
                $percent1_Arr[$gameid] = intval(getvar("percent1"));
                $percent2_Arr[$gameid] = intval(getvar("percent2"));
                $percent_sw_Arr[$gameid] = getvar("percent_sw") ? 1 : 0;
            }
            else if ($g_init_mode_3d && $gameid && $g_menu_enabled[$gameid])
            {
                switch ($gameid)
                {
                    case 1 :
                        break;
                    case 2 :
                        $amount_Arr['3d'] = intval(getvar("amount_3d"));
                        break;
                    default :
                        $amount_Arr['3s'] = intval(getvar("amount_3s1"));
                        break;
                }
				$en_tmp=($gameid<=2?'':'_'.$g_gametype_3d[12]['en']);
                $snow_mod_Arr[$gameid] = getvar("snow_mod".$en_tmp) ? 1 : 0;
                $protmod_Arr[$gameid] = getvar("protmod".$en_tmp) ? 1 : 0;
                $mpercent_Arr[$gameid] = intval(getvar("mpercent".$en_tmp));
                $percent0_Arr[$gameid] = intval(getvar("percent0".$en_tmp));
                $percent1_Arr[$gameid] = intval(getvar("percent1".$en_tmp));
                $percent2_Arr[$gameid] = intval(getvar("percent2".$en_tmp));
                $percent_sw_Arr[$gameid] = getvar("percent_sw".$en_tmp) ? 1 : 0;
            }
            else
            {
                $g_gametype_3d_Enabled = 0;
            }
            if ($g_gametype_3d_Enabled)
            {
                if ($acl == 2)
                {
                    $percent1_Arr[$gameid] = $percent0_Arr[$gameid];
                }
                $chkArr = array();
                $chkArr['percent0'] = $percent0_Arr[$gameid];
                $chkArr['percent1'] = $percent1_Arr[$gameid];
                $chkArr['percent2'] = $percent2_Arr[$gameid];
                $chkArr['mpercent'] = $mpercent_Arr[$gameid];
                $chkArr['percent_sw'] = $percent_sw_Arr[$gameid];
                $ismodify = check_percent_ismodify($id, $gameid, $chkArr);
               
                if ($val['type'] == 1)
                {
                    $chk3DFlagArr['enabled'] = 1;
                    $chk3DFlagArr['totalAmounts'] += $amount_used_Arr[$gameid];
                }
                else if (1 < $val['type'])
                {
                    $chk3SFlagArr['enabled'] = 1;
                    $chk3SFlagArr['totalAmounts'] += $amount_used_Arr[$gameid];
                }
                if (0 < $percent0_Arr[$gameid] + $percent1_Arr[$gameid] + $mpercent_Arr[$gameid] && ( $percent1_Arr[$gameid] < $percent0_Arr[$gameid] || $u_prev_percent_Arr[$gameid]['mpercent'] < $percent0_Arr[$gameid] + $mpercent_Arr[$gameid] ))
                {
                    diemsg("(153){$g_gametype_3d[$gameid]['chs']}占成数设置错误！");
                }
                if ($u_prev_percent_Arr[$gameid]['mpercent'] < $mpercent_Arr[$gameid])
                {
                    diemsg("(157){$g_gametype_3d[$gameid]['chs']}{$acltitle}实际占成不可超过{$u_prev_percent_Arr[$gameid]['mpercent']}！");
                }
                if ($u_prev_percent_Arr[$gameid]['mpercent'] < $percent2_Arr[$gameid])
                {
                    diemsg("(162){$g_gametype_3d[$gameid]['chs']}{$acltitle}收飞占成不可超过{$u_prev_percent_Arr[$gameid]['mpercent']}！");
                }
                if (!Check_Percent($id, $gameid, $mpercent_Arr[$gameid]))
                {
                    diemsg("(141){$g_gametype_3d[$gameid]['chs']}占成设置，不可小於下线占成!");
                }
                if (2 < $acl && get_all_upercent($id, $acl, $percent0_Arr[$gameid], $percent1_Arr[$gameid], $mpercent_Arr[$gameid], $gameid))
                {
                    diemsg("(170){$g_gametype_3d[$gameid]['chs']} {$acltitle}线的占成数设置错误！");
                }
            }
        }
        $rs0 = getdb("select * from k_user where acl=" . $acl . " and id=" . $id);
        if ($rs0->eof)
        {
            diemsg("错误的{$acltitle}编号！");
        }
        if (!( date("w") == $editpercentwod || $editpercentwod == 7 || $superadmin || date("Ymd", $rs0->f['createdt']) == date("Ymd", systime()) && array_sum($amount_used_Arr) == 0 ) && $ismodify)
        {
            diemsg("(182)非可修改占成日，不得修改占成");
        }
       /* if ($g_init_mode_6h && $psen_edit_mod_Arr[0] == 0 && $amount_used_Arr[0] && $amount_Arr['6h'] < $amount_used_Arr[0])
        {
            diemsg("(189)下级会员在本期的 六合彩 已有投注，在开盘期间信用额度不得低於：" . $amount_used_Arr[0]);
        }
        if ($g_init_mode_3d && $chk3DFlagArr['totalAmounts'] && $amount_Arr['3d'] < $chk3DFlagArr['totalAmounts'])
        {
            diemsg("(194)下级会员在本期的 福体彩 已有投注，在开盘期间信用额度不得低於：" . $chk3DFlagArr['totalAmounts']);
        }
        if ($g_init_mode_3d && $chk3SFlagArr['totalAmounts'] && $amount_Arr['3s'] < $chk3SFlagArr['totalAmounts'])
        {
            diemsg("(199)下级会员在本期的 时时彩 已有投注，在开盘期间信用额度不得低於：" . $chk3SFlagArr['totalAmounts']);
        }
        if ($acl == 5 && $super_account != "")
        {
            $rsx = getdb("select * from k_user where acl=255 and account='" . $super_account . "'");
            if ($rsx->eof)
            {
                diemsg("没有这个总监帐号！");
            }
            $rsx->close();
        }
        if ($acl == 5 && array_sum($psen_edit_mod_Arr) < 9 && 0 < array_sum($amount_used_Arr) && $super_account != $rs0->f['supe_account'])
        {
            diemsg("开盘投注期间内不得变更该大股东线的归属总监参数！");
        }*/
        if ($acl == 5 && $gameinit_mode != $rs0->f['init_start_mode'] && 0 < array_sum($amount_used_Arr))
        {
            diemsg("下线会员已有投注，在本日结束之前无法变更博彩类别！");
        }
        if ($pass != "")
        {
            if (G_ACCOUNT == 0)
            {
                if (!ctype_alnum($pass))
                {
                    diemsg("密码只能输入A-Z,a-z,0-9的字元！");
                }
            }
            else
            {
                $pass = chk_pass($pass);
            }
        }
		
        if (isset($maxamount_Arr))
        {
            if (intval($maxamount_Arr['6h']) < $amount_Arr['6h'])
            {
                diemsg("(231)六合彩 " . L_CREDITOVER);
            }
            /*if ($g_init_mode_3d && $chk3DFlagArr['enabled'] && intval($maxamount_Arr['3d']) < $amount_Arr['3d'])
            {
                diemsg("(232)3D彩 " . L_CREDITOVER);
            }
            if ($g_init_mode_3d && $chk3SFlagArr['enabled'] && intval($maxamount_Arr['3s']) < $amount_Arr['3s'])
            {
                diemsg("(233)时时彩 " . L_CREDITOVER);
            }*/
        }
        if ($amount_Arr['6h'] != $rs0->f['amount'])
        {
            AdjustAmount($id, $amount_Arr['6h']);
        }
        /*if ($g_init_mode_3d && $amount_Arr['3d'] != $rs0->f['amount_3d'] && $chk3DFlagArr['enabled'])
        {
            AdjustAmount($id, $amount_Arr['3d'], "_3d");
        }
        if ($g_init_mode_3d && $amount_Arr['3s'] != $rs0->f['amount_3s1'] && $chk3SFlagArr['enabled'])
        {
            AdjustAmount($id, $amount_Arr['3s'], "_3s1");
        }*/
        foreach ($g_gametype_3d as $gameid => $val)
        {
            if ($g_init_mode_6h && $gameid == 0 || $g_init_mode_3d && $gameid && $g_menu_enabled[$gameid])
            {
                switch ($gameid)
                {
                    case 0 :
                        $amount_old = intval($rs0->f['amount']);
                        $amount_new = $amount_Arr['6h'];
                        break;
                    case 2 :
                    case 1 :
                        $amount_old = intval($rs0->f['amount_3d']);
                        $amount_new = $amount_Arr['3d'];
                        break;
                    default :
                        $amount_old = intval($rs0->f['amount_3s1']);
                        $amount_new = $amount_Arr['3s'];
                        break;
                }
                $data = array("dt" => systime(), "account1" => isset($u_prev_user) ? QuotedStr($u_prev_user['account']) : QuotedStr("Admin"), "name1" => isset($u_prev_user) ? QuotedStr($u_prev_user['name']) : QuotedStr("Admin"), "acl1" => isset($u_prev_user) ? $u_prev_user['acl'] : 255, "account2" => QuotedStr($rs0->f['account']), "name2" => QuotedStr($username), "acl2" => $acl, "percent0_old" => 0, "percent0_new" => $percent0_Arr[$gameid], "percent1_old" => 0, "percent1_new" => $percent1_Arr[$gameid], "mpercent_old" => 0, "mpercent_new" => $mpercent_Arr[$gameid], "percent_mode" => $gameid, "modify" => QuotedStr("Admin"), "amount_old" => $amount_old, "amount_new" => $amount_new);
                set_modifybook($id, $gameid, $data);
                $k_percent_data = array("percent0" => $percent0_Arr[$gameid], "percent1" => $percent1_Arr[$gameid], "percent2" => $percent2_Arr[$gameid], "mpercent" => $mpercent_Arr[$gameid], "percent_sw" => $percent_sw_Arr[$gameid], "snow_open" => $snow_mod_Arr[$gameid], "protmod" => $protmod_Arr[$gameid]);
                db_update("k_user_percent", $k_percent_data, "user_id={$id} and gametype={$gameid}");
                if ($percent_sw_Arr[$gameid])
                {
                    Max_Percent($rs0, $gameid, $mpercent_Arr[$gameid], $gameid);
                }
            }
        }
        $tmp = array();
        $tmp['name'] = QuotedStr($username);
        $tmp['disabled'] = $mbr_disabled_chk;
        //if ($g_init_mode_6h)
        //{
            $tmp['amount'] = $amount_Arr['6h'];
            if (isset($reat0_open) && $reat0_open)
            {
                $tmp['rate0'] = $rate0;
                $tmp['reat0_open'] = $rate0mod;
                $tmp['reat0_8_open'] = $rate8mod;
            }
        //}
        /*if ($g_init_mode_3d && $chk3DFlagArr['enabled'])
        {
            $tmp['amount_3d'] = $amount_Arr['3d'];
        }
        if ($g_init_mode_3d && $chk3SFlagArr['enabled'])
        {
            $tmp['amount_3s1'] = $amount_Arr['3s'];
        }*/
        /*if ($acl == 5 && isset($super_account))
        {
            $tmp['supe_account'] = QuotedStr($super_account);
        }*/
        if ($acl == 5 && isset($gameinit_mode))
        {
            $tmp['init_start_mode'] = $gameinit_mode;
        }
        $tmp['updatedt'] = systime();
        if ($pass != "")
        {
            $tmp['pass'] = QuotedStr(md5($pass));
            $tmp['chgpwdt'] = 0;
        }
        db_update("k_user", $tmp, "acl=" . $acl . " and id=" . $id);
        $diskmod = array(1 => 0, 2 => 0, 3 => 0, 4 => 0);
        $diskmod[1] = $disk_chg->trans_on(getvar("disk1mod"));
        $diskmod[2] = $disk_chg->trans_on(getvar("disk2mod"));
        $diskmod[3] = $disk_chg->trans_on(getvar("disk3mod"));
        $diskmod[4] = $disk_chg->trans_on(getvar("disk4mod"));
        $disk_data = array("id" => $id, "acl" => $acl, "prev_id" => $prev_id, "disk1" => $diskmod[1], "disk2" => $diskmod[2], "disk3" => $diskmod[3], "disk4" => $diskmod[4]);
        $disk_chg->Update($disk_data);
        if ($rs0->f['enabled'] != $enabled)
        {
            if ($enabled == 1)
            {
                if (!$GLOBALS['user']->enable($id))
                {
                    $tpl->set("memo", "<font color=red>" . $GLOBALS['g_cache']['user_error']['string'] . "</font>");
                }
            }
            else if ($enabled == 2)
            {
                $GLOBALS['user']->pause($id);
            }
            else if (0 < array_sum($amount_used_Arr))
            {
                diemsg("此位{$acltitle}有会员在本期已经有投注，在本日结束之前不得停用本{$acltitle}！");
            }
            else
            {
                $GLOBALS['user']->disable($id);
            }
        }
        if ($acl == 5 && $gameinit_mode != $rs0->f['init_start_mode'] && array_sum($amount_used_Arr) == 0)
        {
            $users = GetUserList($id);
            if ($users != "")
            {
                $rsx = getdb("select id from k_user where id in (" . $users . ") and enabled>0");
                while (!$rsx->eof)
                {
                    $rsz = getdb("select uid from k_sessions where uid=" . $rsx->f['id']);
                    if (!$rsz->eof)
                    {
                        getdb("delete from k_sessions where uid=" . $rsx->f['id'] . " and sid<>'" . session_id() . "'");
                    }
                    $rsz->close();
                    $rsx->movenext();
                }
            }
        }
    }
    else
    {
        $aclFirstWordArr = array(1 => "d", 2 => "c", 3 => "b", 4 => "a", 5 => "s");
        if (G_ACCOUNT == 0)
        {
            if ($account == "")
            {
                diemsg(L_INPUTACCOUNT);
            }
            if (!ctype_alnum($account))
            {
                diemsg("帐号只能输入A-Z,a-z,0-9的字元！");
            }
            if (substr($account, 0, 1) != $aclFirstWordArr[$acl])
            {
                diemsg("{$acltitle}帐号前置码必需为 {$aclFirstWordArr[$acl]} ");
            }
            if (strlen($account) < 5 - intval($acl) + 2)
            {
                diemsg("帐号至少需要" . ( 5 - intval($acl) + 2 ) . "码以上！");
            }
            if ($pass == "")
            {
                diemsg(L_INPUTPWD);
            }
            if (!ctype_alnum($pass))
            {
                diemsg("密码只能输入A-Z,a-z,0-9的字元！");
            }
        }
        else
        {
            $account = chk_account($account);
            $pass = chk_pass($pass);
        }
        $rs = getdb("select * from k_user where account='" . $account . "'");
        if (!$rs->eof)
        {
            diemsg("帐号重复，请使用另外的帐号！");
        }
        /*if ($acl == 5 && isset($super_account) && $super_account != "")
        {
            $rsx = getdb("select * from k_user where acl=255 and account='" . $super_account . "'");
            if ($rsx->eof)
            {
                diemsg("没有这个总监帐号！");
            }
            $rsx->close();
        }*/
        $ins_diskmod[1] = $disk_chg->trans_on(getvar("disk1mod"));
        $ins_diskmod[2] = $disk_chg->trans_on(getvar("disk2mod"));
        $ins_diskmod[3] = $disk_chg->trans_on(getvar("disk3mod"));
        $ins_diskmod[4] = $disk_chg->trans_on(getvar("disk4mod"));
        $data = array("acl" => $acl, "enabled" => $enabled, "account" => QuotedStr($account), "pass" => QuotedStr(md5($pass)), "name" => QuotedStr($username), "disk1" => $ins_diskmod[1], "disk2" => $ins_diskmod[2], "disk3" => $ins_diskmod[3], "disk4" => $ins_diskmod[4], "createdt" => systime(), "prev_id" => $prev_id, "createAccount" => QuotedStr($curruser['account'] == G_SUPERACCOUNT ? "Admin" : $curruser['account'] ), "disabled" => $mbr_disabled_chk);
        if ($acl == 5)
        {
            //$data['supe_account'] = QuotedStr($super_account);
            $data['init_start_mode'] = $gameinit_mode;
        }
        $ins_chk = db_insert("k_user", $data);
        if ($ins_chk == TRUE)
        {
            $tpl->set("ins_chk", "ins");
        }
        $sql = "!!select id from k_user where acl=" . $acl . " and account='" . $account . "' and pass='" . md5($pass) . "'";
        $rs = getdb($sql);
        if (!$rs->eof)
        {
            $id = $rs->f[0];
            CreatePercents($id, 0 - 1, $u_prev_percent_Arr);
            foreach ($g_gametype_3d as $gameid => $val)
            {
                CreateAmount($id, $gameid);
            }
        }
        else
        {
            diemsg("错误的新增数据造成储存错误！");
        }
    }
}
else
{
    echo "没有修改权限<br>";
}
?>